package logic;

import editor.Grid;
import editor.Menu.OGGETTI;
import gameObjects.Bike.POINTTO;
import gameObjects.BikeCOM.TURNDIRECTION;
import arena.ArenaGS;

public class FollowPalyer {
	
	public enum Position {PRIMO,SECONDO,TERZO,QUARTO,NULL}
	private OGGETTI[][]  cells = Grid.getGrid();
	Position pos;
	public TURNDIRECTION follow(float x,float y, POINTTO direction)
	{
		
		int posX,posY;
		int humanPosX=(int)ArenaGS.getInstance().getHuman().getPosX();
		int humanPosY=(int)ArenaGS.getInstance().getHuman().getPosZ();
		pos=this.getPositio(x, y, humanPosX, humanPosY);
		System.out.println("TI VIGNU PIGLIA "+humanPosX+" "+humanPosY);
		System.out.println("POSITIO: "+pos);
		switch(direction)
		{
		case NORTH:
			posX=(int)x;
			posY=(int)y;
			if(pos==Position.PRIMO || pos== Position.SECONDO)
			{
				if(posY==humanPosY)
				{
					if(posX>humanPosX)
					{
						if(cells[(int)posX-1][(int)posY]!=OGGETTI.WALL && cells[(int)posX-1][(int)posY]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.RIGHT;
					}
					if(posX<humanPosX){
						if(cells[(int)posX+1][(int)posY]!=OGGETTI.WALL && cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.LEFT;
					}
				}
				return TURNDIRECTION.AWAY;
			}
			else
			{
				if(pos==Position.TERZO &&(cells[(int)posX+1][(int)posY]!=OGGETTI.WALL && cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL))
					return TURNDIRECTION.LEFT;
				if(pos==Position.QUARTO && (cells[(int)posX+1][(int)posY]!=OGGETTI.WALL && cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL))
					return TURNDIRECTION.RIGHT;
			}
			break;
		case SOUTH:
			posX=(int)x;
			posY=(int)y;
			if(pos==Position.TERZO || pos==Position.QUARTO)
			{
				if(posY==humanPosY)
				{
					if(posX>humanPosX)
					{
						if(cells[(int)posX-1][(int)posY]!=OGGETTI.WALL && cells[(int)posX-1][(int)posY]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.LEFT;
					}
					if(posX<humanPosX)
					{
						if(cells[(int)posX+1][(int)posY]!=OGGETTI.WALL && cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.RIGHT;
					}
				}
				return TURNDIRECTION.AWAY;
			}
			else
			{
				if(pos==Position.PRIMO && (cells[(int)posX+1][(int)posY]!=OGGETTI.WALL && cells[(int)posX+1][(int)posY]!=OGGETTI.EXTWALL))
					return TURNDIRECTION.RIGHT;
				if(pos==Position.SECONDO && (cells[(int)posX-1][(int)posY]!=OGGETTI.WALL && cells[(int)posX-1][(int)posY]!=OGGETTI.EXTWALL))
					return TURNDIRECTION.LEFT;
					
			}
			break;
		case EAST:
			posX=(int)x;
			posY=(int)y;
			if(pos==Position.SECONDO || pos==Position.QUARTO)
			{
				if(posX==humanPosX)
				{
					if(posY>humanPosY)
					{
						if(cells[(int)posX][(int)posY-1]!=OGGETTI.WALL && cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.RIGHT;
					}
					if(posY<humanPosY)
					{
						if(cells[(int)posX][(int)posY+1]!=OGGETTI.WALL && cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL)
						return TURNDIRECTION.LEFT;
					}
				}
				return TURNDIRECTION.AWAY;
			}
			else 
			{
				System.out.println("TERZZO/QUARTO EAST");
				if(pos==Position.PRIMO && (cells[(int)posX][(int)posY+1]!=OGGETTI.WALL && cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL))
					return TURNDIRECTION.LEFT;
				if(pos==Position.TERZO && (cells[(int)posX][(int)posY-1]!=OGGETTI.WALL && cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL))
					return TURNDIRECTION.RIGHT;
			}
			break;
		case WEST:
			posX=(int)x;
			posY=(int)y;
			if(pos==Position.PRIMO || pos==Position.TERZO)
			{
				if(posX==humanPosX)
				{
					if(posY>humanPosY)
					{
						if(cells[(int)posX][(int)posY-1]!=OGGETTI.WALL && cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.LEFT;
					}
					if(posY<humanPosY)
					{
						if(cells[(int)posX][(int)posY+1]!=OGGETTI.WALL && cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL)
							return TURNDIRECTION.RIGHT;
					}
				}
				return TURNDIRECTION.AWAY;
			}
			else 
			{
				System.out.println("TERZZO/QUARTO EAST");
				if(pos==Position.SECONDO && (cells[(int)posX][(int)posY+1]!=OGGETTI.WALL && cells[(int)posX][(int)posY+1]!=OGGETTI.EXTWALL))
					return TURNDIRECTION.RIGHT;
				if(pos==Position.QUARTO && (cells[(int)posX][(int)posY-1]!=OGGETTI.WALL && cells[(int)posX][(int)posY-1]!=OGGETTI.EXTWALL))
					return TURNDIRECTION.LEFT;
			}
			break;
		}//chiude switch
		return TURNDIRECTION.AWAY;
	}
	
	private Position getPositio(float posX,float posZ, float humanPosX, float humanPosZ)
	{
		if(posZ<=humanPosZ)
		{
			if(posX<=humanPosX)
				return Position.PRIMO;
			if(posX>humanPosX)
				return Position.SECONDO;
		}
		if(posZ>humanPosZ)
		{
			if(posX<=humanPosX)
				return Position.TERZO;
			if(posX>humanPosX)
				return Position.QUARTO;
		}
		return Position.NULL;
	}
	 

}
